using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Data.SqlClient;
using Entities;
/// <summary>
/// Summary description for KhoaHocDAL
/// </summary>
namespace DataAccess
{

    public class KhoaHocDAL : BaseDAL
    {
        #region Private Variables

        #endregion

        #region Public Constructors
        public KhoaHocDAL()
        {
            //
            // TODO: Add constructor logic here
            //

        }
        #endregion



        #region Public Methods
        public int Insert(KhoaHoc objKhoaHoc)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "sp_sp_KhoaHoc_Insert";
            SqlParameter Sqlparam;

            Sqlparam = new SqlParameter("@MaKhoaHoc", SqlDbType.NChar);
            Sqlparam.Value = objKhoaHoc.MaKhoaHoc;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@TenKhoaHoc", SqlDbType.NVarChar);
            Sqlparam.Value = objKhoaHoc.TenKhoaHoc;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@NgayBatDau", SqlDbType.DateTime);
            Sqlparam.Value = objKhoaHoc.NgayBatDau;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@NgayKetThuc", SqlDbType.DateTime);
            Sqlparam.Value = objKhoaHoc.NgayKetThuc;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@ID", SqlDbType.Int);
            Sqlparam.Direction = ParameterDirection.ReturnValue;
            Sqlcomm.Parameters.Add(Sqlparam);


            int result = base.ExecuteNoneQuery(Sqlcomm);

            if (!Convert.IsDBNull(Sqlcomm.Parameters["@ID"]))
                result = Convert.ToInt32(Sqlcomm.Parameters["@ID"].Value);

            return result;
        }

        public int Update(KhoaHoc objKhoaHoc)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "sp_sp_KhoaHoc_UpdateByPK";
            SqlParameter Sqlparam;

            Sqlparam = new SqlParameter("@Khoa", SqlDbType.BigInt);
            Sqlparam.Value = objKhoaHoc.Khoa;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@MaKhoaHoc", SqlDbType.NChar);
            Sqlparam.Value = objKhoaHoc.MaKhoaHoc;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@TenKhoaHoc", SqlDbType.NVarChar);
            Sqlparam.Value = objKhoaHoc.TenKhoaHoc;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@NgayBatDau", SqlDbType.DateTime);
            Sqlparam.Value = objKhoaHoc.NgayBatDau;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@NgayKetThuc", SqlDbType.DateTime);
            Sqlparam.Value = objKhoaHoc.NgayKetThuc;
            Sqlcomm.Parameters.Add(Sqlparam);



            Sqlparam = new SqlParameter("@ErrorCode", SqlDbType.Int);
            Sqlparam.Direction = ParameterDirection.ReturnValue;
            Sqlcomm.Parameters.Add(Sqlparam);

            int result = base.ExecuteNoneQuery(Sqlcomm);

            if (!Convert.IsDBNull(Sqlcomm.Parameters["@ErrorCode"]))
                result = Convert.ToInt32(Sqlcomm.Parameters["@ErrorCode"].Value);

            return result;


        }

        public int Delete(KhoaHoc objKhoaHoc)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "sp_KhoaHoc_DeleteByPK";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@Khoa", SqlDbType.BigInt);
            Sqlparam.Value = objKhoaHoc.Khoa;
            Sqlcomm.Parameters.Add(Sqlparam);



            int result = base.ExecuteNoneQuery(Sqlcomm);
            return result;
        }

        public int DeleteAll()
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "sp_KhoaHoc_DeleteAll";

            int result = base.ExecuteNoneQuery(Sqlcomm);
            return result;
        }

        public KhoaHoc Select(KhoaHoc objKhoaHoc)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "sp_KhoaHoc_GetByPK";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@Khoa", SqlDbType.BigInt);
            Sqlparam.Value = objKhoaHoc.Khoa;
            Sqlcomm.Parameters.Add(Sqlparam);



            DataSet ds = base.GetDataSet(Sqlcomm);
            DataRow dr = null;
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                dr = ds.Tables[0].Rows[0];
                if (!Convert.IsDBNull(dr["Khoa"]))
                    objKhoaHoc.Khoa = Convert.ToInt64(dr["Khoa"]);
                if (!Convert.IsDBNull(dr["MaKhoaHoc"]))
                    objKhoaHoc.MaKhoaHoc = Convert.ToString(dr["MaKhoaHoc"]);
                if (!Convert.IsDBNull(dr["TenKhoaHoc"]))
                    objKhoaHoc.TenKhoaHoc = Convert.ToString(dr["TenKhoaHoc"]);
                if (!Convert.IsDBNull(dr["NgayBatDau"]))
                    objKhoaHoc.NgayBatDau = Convert.ToDateTime(dr["NgayBatDau"]);
                if (!Convert.IsDBNull(dr["NgayKetThuc"]))
                    objKhoaHoc.NgayKetThuc = Convert.ToDateTime(dr["NgayKetThuc"]);

            }
            return objKhoaHoc;
        }

        public ArrayList SelectAllToArray()
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "sp_KhoaHoc_GetAll";

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;
            ArrayList arrKhoaHoc = new ArrayList();
            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
                foreach (DataRow dr in dt.Rows)
                {
                    KhoaHoc objKhoaHoc = new KhoaHoc();
                    if (!Convert.IsDBNull(dr["Khoa"]))
                        objKhoaHoc.Khoa = Convert.ToInt64(dr["Khoa"]);
                    if (!Convert.IsDBNull(dr["MaKhoaHoc"]))
                        objKhoaHoc.MaKhoaHoc = Convert.ToString(dr["MaKhoaHoc"]);
                    if (!Convert.IsDBNull(dr["TenKhoaHoc"]))
                        objKhoaHoc.TenKhoaHoc = Convert.ToString(dr["TenKhoaHoc"]);
                    if (!Convert.IsDBNull(dr["NgayBatDau"]))
                        objKhoaHoc.NgayBatDau = Convert.ToDateTime(dr["NgayBatDau"]);
                    if (!Convert.IsDBNull(dr["NgayKetThuc"]))
                        objKhoaHoc.NgayKetThuc = Convert.ToDateTime(dr["NgayKetThuc"]);
                    arrKhoaHoc.Add(objKhoaHoc);
                }
            }
            return arrKhoaHoc;
        }

        public DataTable SelectAll()
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "sp_KhoaHoc_GetAll";

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;

            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

            }
            return dt;
        }


        #endregion

    }

}
